package ac.cns.inchoen.UIandroid;

import java.util.ArrayList;

import ac.cns.incheon.Adapter.CalViewCustomAdapter;
import ac.cns.incheon.model.CalViewListViewModel;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class CalViewActivity extends Activity {
	private String straddSub = null;
	private String straddCre = null;
	private String straddGrade = null;
	private float sumCre = 0;
	private float sumGrade = 0;
	private String result;

	private ArrayList<CalViewListViewModel> list = new ArrayList<CalViewListViewModel>();
	private CalViewCustomAdapter adapter = null;
	private CalViewListViewModel model = null;
	private EditText addSub = null;
	private EditText addCre = null;
	private EditText addGrade = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cal_view);
	

		Button addButton = (Button) findViewById(R.id.add_button);
		Button removeButton = (Button) findViewById(R.id.remove_button);

		adapter = new CalViewCustomAdapter(this, list);

		ListView listView = (ListView) findViewById(R.id.activity_cal_view_listview);
		listView.setAdapter(adapter);

		addButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub

				addSub = (EditText) findViewById(R.id.sub_editText);
				addCre = (EditText) findViewById(R.id.cre_editText);
				addGrade = (EditText) findViewById(R.id.grade_editText);
				straddSub = addSub.getText().toString();
				straddCre = addCre.getText().toString();
				straddGrade = addGrade.getText().toString();

				sumCre = sumCre + Integer.parseInt(straddCre);
				sumGrade = sumGrade + Float.parseFloat(straddCre)
						* Float.parseFloat(straddGrade);
				String result = String.format("%.2f", sumGrade / sumCre);

				TextView resultGrade = (TextView) findViewById(R.id.result_textview);
				resultGrade.setText(result);

				model = new CalViewListViewModel();
				model.setSub(straddSub);
				model.setCre(straddCre);
				model.setGrade(straddGrade);

				list.add(model);
				adapter.notifyDataSetChanged();

			}
		});

		removeButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (list.size() > 0) {
					straddSub = addSub.getText().toString();
					straddCre = addCre.getText().toString();
					straddGrade = addGrade.getText().toString();

					sumCre = sumCre - Integer.parseInt(straddCre);
					sumGrade = sumGrade - Float.parseFloat(straddCre)
							* Float.parseFloat(straddGrade);
					String result = String.format("%.2f", sumGrade / sumCre);

					TextView resultGrade = (TextView) findViewById(R.id.result_textview);
					resultGrade.setText(result);
					
					list.remove(list.size() - 1);
					adapter.notifyDataSetChanged();
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_cal_view, menu);
		return true;
	}

}
